<template>
  <div style="overflow-y: auto;  height: 100%;">
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="80px" style="overflow: hidden">
      <el-form-item label="文件名称" prop="title">
        <el-input v-model="formData.title" placeholder="请输入文件名称" clearable :style="{width: disabled?'100%':'calc(100% - 95px)'}" :disabled="disabled"></el-input>
        <el-upload
                v-if="!disabled"
                class="upload-demo"
                :action="mainAction+'?type=2'"
                :on-success="mainUploadSuccess"
                :on-progress="uploadProgress"
                style="display: inline-block; margin-left: 10px"
                :show-file-list="false">
          <el-button size="small" type="primary">上传正文</el-button>
        </el-upload>
        <div style="font-size: 12px; color: #de4c4a">注：严禁上传涉密文件（限50M以内文件）</div>
      </el-form-item>
      <el-form-item label="来文单位" prop="sender" >
        <el-input v-model="formData.sender" placeholder="请输入来文单位" clearable :style="{width: '100%'}" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="收文日期" prop="collect_date">
        <el-date-picker
                v-model="formData.collect_date"
                type="date"
                clearable :style="{width: '100%'}"
                value-format="yyyy-MM-dd"
                :disabled="disabled"
                placeholder="请选择收文日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="收文文号" prop="collect_txt">
        <el-input v-model="formData.collect_txt" placeholder="请输入收文文号" clearable :style="{width: '100%'}" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="紧急程度" prop="level">
        <el-radio-group v-model="formData.level">
          <el-radio :label="0">一般</el-radio>
          <el-radio :label="1">紧急</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="formData.remarks" placeholder="请输入备注" clearable :style="{width: '100%'}" :disabled="disabled"></el-input>
      </el-form-item>
      <el-form-item label="拟稿单位" prop="dept_id" >
        <dept-select-tree dept-type="my"  v-model="formData.dept_id" placeholder="请选择部门" :disabled="disabled"></dept-select-tree>
      </el-form-item>
      <el-form-item label="拟稿人" prop="alias">
        <el-input v-model="formData.alias" placeholder="请输入合同名称" clearable :style="{width: '100%'}" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="上传附件" prop="files">
        <my-upload-file desc="若上传EXCEL文件，请先调整打印模式以便后续预览" v-model="formData.files">
        </my-upload-file>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import api from "@/api/index";
  import eventBus from "@/utils/eventBus";
  import FileMove from "../../../components/Common/FileMove";
  import DeptSelectTree from "@/components/Common/DeptSelectTree";
  import {mapGetters} from "vuex";
  import MyUploadFile from "../../../components/Common/MyUploadFile";
export default {
  name:"LeftFormSignature",
  components: {MyUploadFile, DeptSelectTree, FileMove},
  props: ["row","disabled"],
  data() {
    return {
      formData: {
        title: undefined,
        files: null,
        is_draft:0,
        sender:undefined,
        collect_date:undefined,
        collect_txt:"",
        text_file:"",
        type_id:2, //公文类型 1.校内签呈 2.学校收文 3.学校发文,
        dept_id:undefined,
        alias:"",
        remarks:"",
        recipient:[],
        year:new Date().getFullYear(),
        num:undefined,
        level:0,
        is_edit:false
      },
      rules: {
        title: [{
          required: true,
          message: '请输入文件名称',
          trigger: 'blur'
        }],
        collect_date: [{
          required: true,
          message: '请选择收文日期',
          trigger: 'blur'
        }],
      },
      fileNamefileList: [],
      templateList:[],
    }
  },
  computed: {
    ...mapGetters(["userinfo"]),
    mainAction() {
      return process.env.VUE_APP_BASE_API + "/api/document/upload_file/";
    },
  },
  created() {
    this.getInfo()
  },
  mounted() {
    eventBus.$on("recipient",(recipient)=>{
      this.formData.recipient = recipient
    })
  },
  methods: {
    uploadProgress(){
      eventBus.$emit("wordloading")
    },
    getInfo(){
      if(this.row&&this.row.id){
        this.formData.id = this.row.id
        for(var o in this.row){
          this.formData[o] = this.row[o]
        }
      }else{
        this.formData.alias = this.userinfo.alias;
      }
    },
    mainUploadSuccess(res,file){
      if(res.code == 0){
        this.formData.text_file = res.data.wopi
        let index = file.name.lastIndexOf(".")
        this.formData.title = file.name.substring(0,index)
        eventBus.$emit("openWord",res.data.wopi)
      }else {
        this.msgError(res.msg)
      }
    },
    submitForm() {
      let self = this
      return new Promise(function(resolve, reject) {
        self.$refs['elForm'].validate(valid => {
          if (!valid) return
          if(!self.formData.text_file){
            self.msgError("正文不允许为空！");
            return;
          }
          resolve(self.formData)
        })
      });
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
  }
}

</script>
<style lang="scss" scoped>
.el-upload__tip {
  line-height: 1.2;
}
</style>
